package shin.P1.com;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import shin.P1.com.TestDTO;

@Controller
@SessionAttributes("testDTO")
public class HomeController{
	@Autowired
	private UserService userService;
	@Autowired
	private SqlSession sqlSession;
	
	@RequestMapping(value = "/")
	public String home() {
		return "home";
	}
	@RequestMapping(value = "/login", method=RequestMethod.POST)
	public String index(TestDTO testDTO, HttpSession session) {
		String id = testDTO.getId();
		session.setAttribute("id", id);
		HashMap<String, String> input = new HashMap<String, String>();
        input.put("code", "shin");
        List<HashMap<String, String>> outputs = sqlSession.selectList("shin.P1.com.UserService.selectSample", input);
        System.out.print(outputs.toString());

		return "home";
	}
	
	
	@RequestMapping(value = "/logout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "home";
	}
	
	@RequestMapping(value = "/join")
	public String join(HttpSession session) {
		session.invalidate();
		return "join";
	}
	
	@RequestMapping(value = "/test")
	public String test(HttpSession session) {
		
		try {
		    System.out.println("Loading driver...");
		    Class.forName("com.mysql.jdbc.Driver");
		    System.out.println("Driver loaded!");
		} catch (ClassNotFoundException e) {
		    throw new RuntimeException("Cannot find the driver in the classpath!", e);
		}
		
		return "home";
	}


	
}
